﻿function CheckAll(oCheckbox, gridId, hid) {
    var grid = document.getElementById(gridId);
    if (grid.rows.length > 2) {
        for (i = 1; i < grid.rows.length - 1; i++) {
            var cb = grid.rows[i].cells[0].getElementsByTagName("INPUT")[0];
            if (cb) {
                cb.checked = oCheckbox.checked;
                if (grid.rows[i].cells[0].getElementsByTagName("input")[1] != null) {
                    SetHidValue(cb, hid, grid.rows[i].cells[0].getElementsByTagName("input")[1].value);
                }
            }
        }
    }
}

function SetHidValue(cb, hid, tValue) {
    var result = document.getElementById(hid).value;
    result = (result == null) ? "" : result;

    var objArray;
    if (result == "") {
        objArray = new Array();
    }
    else {
        objArray = result.split(';');
    }
    var len = objArray.length;

    if (cb.checked) {
        var count = 0;
        for (var i = 0; i < len; i++) {
            if (objArray[i] != tValue) {
                count++;
            }
        }
        if (count == len) {
            objArray.push(tValue);
        }
    }
    else {
        for (var i = 0; i < len; i++) {
            if (objArray[i] == tValue) {
                objArray.splice(i, 1);
                break;
            }
        }
    }
    document.getElementById(hid).value = objArray.join(';');
}

function CheckItemCB(cb, gridId, hid) {
    var count = 0;
    var grid = document.getElementById(gridId);

    for (i = 1; i < grid.rows.length; i++) {
        var item = grid.rows[i].cells[0].getElementsByTagName("INPUT")[0];
        if (item) {
            if (cb.id == item.id) {
                if (grid.rows[i].cells[0].getElementsByTagName("input")[1] != null) {
                    SetHidValue(cb, hid, grid.rows[i].cells[0].getElementsByTagName("input")[1].value);
                }
            }

            if (item.checked) {
                count++;
            }
        }
        else {
            count++;
        }
    }
    if (count == grid.rows.length - 1) {
        grid.rows[0].cells[0].getElementsByTagName("INPUT")[0].checked = true;
    }
    else {
        grid.rows[0].cells[0].getElementsByTagName("INPUT")[0].checked = false;
    }
}